package poi;

import org.apache.poi.xwpf.usermodel.XWPFTable;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 实现了 IParse 接口的抽象类
 *
 * @author kai
 */
public abstract class BaseDocumentParse<E> implements IDocumentParse<E> {

    protected final List<Pattern> patternList = new ArrayList<>();

    protected final List<Integer> positionList = new ArrayList<>();

    private boolean isParsed = false;
    private int currentIndex = 0;

    @Override
    public E parseDocxParagraph(String paragraph, TempScanData tempScanData) {
        return null;
    }

    @Override
    public Collection<E> parseDoxcTable(XWPFTable xwpfTable, Collection<E> collection) {
        return null;
    }

    @Override
    public final boolean isParsed() {
        return isParsed;
    }

    @Override
    public void setParsed(boolean parsed) {
        isParsed = parsed;
    }

    @Override
    public void setRegex(String... regexStr) {
        patternList.clear();
        for (final String s : regexStr) {
            patternList.add(Pattern.compile(s));
        }
    }

    @Override
    public void setAllPosition(int... array) {
        if (array != null) {
            positionList.clear();
            for (int i : array) {
                positionList.add(i);
            }
        }
    }

    @Override
    public int nextPosition() {
        final int size = positionList.size();
        return positionList.get(currentIndex++ % size);
    }

}
